package com.java.star.mall.front.service.impl;

import com.java.star.mall.common.constant.cache.CategoryCacheConstant;
import com.java.star.mall.front.dao.cache.CategoryRepository;
import com.java.star.mall.front.dao.mapper.CategoryMapper;
import com.java.star.mall.front.pojo.entity.Category;
import com.java.star.mall.front.pojo.vo.CategoryLevel2VO;
import com.java.star.mall.front.pojo.vo.CategoryProductVO;
import com.java.star.mall.front.pojo.vo.CategoryVO;
import com.java.star.mall.front.pojo.vo.ProductVO;
import com.java.star.mall.front.properties.ImageProperties;
import com.java.star.mall.front.service.CategoryService;
import com.java.star.mall.front.service.ProductService;
import io.jsonwebtoken.lang.Collections;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品分类Service实现类
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ProductService productService;

    @Autowired
    private ImageProperties imageProperties;

    @Autowired
    private CategoryRepository categoryRepository;

    @Autowired
    private RedissonClient redissonClient;

    @Override
    public List<CategoryVO> listIndexCategory() {
        //查询缓存
        List<CategoryVO> categoryVOList = categoryRepository.listIndexCategory();
        if (!Collections.isEmpty(categoryVOList)) {
            return categoryVOList;
        }

        //加锁
        RLock lock = redissonClient.getLock(CategoryCacheConstant.INDEX_CATEGORY_LOCK);
        try {
            lock.lock();
            //再次查询缓存
            categoryVOList = categoryRepository.listIndexCategory();
            if (!Collections.isEmpty(categoryVOList)) {
                return categoryVOList;
            }

            //查询10个首页一级分类
            List<Category> indexCategoryList = categoryMapper.listIndexCategory();

            //装配VO内容
            List<CategoryVO> indexCategoryVOList = indexCategoryList.stream().map(category -> {
                CategoryVO categoryVO = new CategoryVO();
                BeanUtils.copyProperties(category, categoryVO);
                categoryVO.setImage(imageProperties.getUrl() + category.getImage());
                return categoryVO;
            }).collect(Collectors.toList());

            //放缓存
            categoryRepository.addIndexCategory(indexCategoryVOList);

            return indexCategoryVOList;
        } finally {
            lock.unlock();
        }
    }

    @Override
    public List<CategoryProductVO> listCategoryProduct() {
        //1.查询所有分类
        List<Category> categoryList = categoryMapper.listAllCategory();

        //2.获取一级分类
        List<Category> leve1CategoryList = getCategoryByParentId(categoryList, 0);

        //3.根据一级分类获取对应的二级分类和二级分类对应的商品数据
        List<CategoryProductVO> categoryProductVOList = leve1CategoryList.stream().map(leve1Category -> {
            //4.过滤出二级分类
            List<Category> leve2CategoryList = getCategoryByParentId(categoryList, leve1Category.getId());

            //5.根据二级分类查询二级分类对应的商品数据
            List<CategoryLevel2VO> categoryLevel2VOList = leve2CategoryList.stream().map(leve2Category -> {
                List<ProductVO> productVOList = productService.listProductByCategoryId(leve2Category.getId());
                //6.装填二级分类VO数据
                CategoryLevel2VO categoryLevel2VO = new CategoryLevel2VO();
                BeanUtils.copyProperties(leve2Category, categoryLevel2VO);
                categoryLevel2VO.setProductList(productVOList);
                return categoryLevel2VO;
            }).collect(Collectors.toList());

            //7.装填分类数据及商品数据
            CategoryProductVO categoryProductVO = new CategoryProductVO();
            BeanUtils.copyProperties(leve1Category, categoryProductVO);
            categoryProductVO.setCategoryLevel2List(categoryLevel2VOList);
            return categoryProductVO;
        }).collect(Collectors.toList());

        return categoryProductVOList;
    }

    /**
     * 根据父级分类id过滤分类信息列表
     *
     * @param categoryList 分类列表
     * @param parentId     父级id
     * @return 过滤后的分类列表
     */
    private List<Category> getCategoryByParentId(List<Category> categoryList, Integer parentId) {
        return categoryList.stream().filter(category -> category.getParentId()
                .equals(parentId)).collect(Collectors.toList());
    }
}
